#!/usr/bin/python3
from logo import logo
import os
import sys
from PyQt5 import QtWidgets
import ui
import random

class GuessingApp(QtWidgets.QMainWindow, ui.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.number = random.randint(1, 100)
        self.start_text = "Hello. It's a Guessing Game\nChoose you difficulty. Type 'easy' or 'hard'"
        self.ask_new_game_text = "Next game? 'y' or 'n'"
        self.setupUi(self)
        self.pushButton.clicked.connect(self.next_step)
        self.rounds = 0
        self.status = 'start'
        self.set_text(self.start_text)

    def read_input(self):
        value = self.textEdit.toPlainText()
        self.textEdit.clear()
        return value

    def set_text(self, text):
        self.textBrowser.setText(text)

    def set_difficulty(self):
        '''Settings'''
        difficulty = self.read_input()
        if difficulty == 'easy':
            rounds = 10
        elif difficulty == 'hard':
            rounds = 5
        else:
            self.set_text(f"Wrong answer. Try again 'easy' or 'hard'")
            return
        self.status = 'game'
        self.rounds = rounds
        self.set_text(f"Starting the game. Try to guess a number from 1 to 100.\nYou have {self.rounds} attempts")

    def next_step(self):
        #self.textBrowser.clear()
        # Game Logic ------------------------
        if self.status == 'over':
            is_another_game = self.read_input()
            if is_another_game == 'y':
                self.rounds = 0
                self.status = 'start'
                self.set_text(self.start_text)
                self.number = random.randint(1, 100)
                return
            else:
                sys.exit(0)
        if self.rounds == 0 and self.status == 'start':
            return self.set_difficulty()
        try:
            guess = int(self.read_input())
        except ValueError:
            guess = False
        if not guess:
            self.textBrowser.setText(f"Wrong number. Try again\nYou have {self.rounds} attempts")
            return
        self.rounds -= 1
        if guess > self.number:
            self.set_text(f'To high\nYou have {self.rounds} attempts')
        elif guess == self.number:
            self.set_text(f'You win! The number is {guess}\n{self.ask_new_game_text}')
            self.game_over()
        else:
            self.set_text(f'To low\nYou have {self.rounds} attempts')
        if self.rounds == 0 and guess != self.number:
            self.set_text(f'You are lose! Answer is {self.number}\n{self.ask_new_game_text}')
            self.game_over()

    def game_over(self):
       self.status = 'over'



def main():
    app = QtWidgets.QApplication(sys.argv)
    window = GuessingApp()
    window.show()
    app.exec_()



if __name__ == '__main__':
    main()




# is_game_over = False
# while not is_game_over:
#     print(f'You have {rounds} attempts')
#     guess = int(input('Make a guess: '))
#     if guess > number:
#         print('To high')
#     elif guess == number:
#         print(f'You are win. The number is {guess}')
#         is_game_over = True
#     else:
#         print('To low')
#     rounds -= 1
#     if rounds == 0 and guess != number:
#         print('You are lose!')
#         is_game_over = True
